(function( _ , $ ){
	_.Html.button = "<div class='jdmer_button' draggable='false'>"+
	"	<div class='btn_left'></div>"+
	"	<div class='btn_label'></div>"+
	"	<div class='btn_right'></div>"+
	"</div>";
	
	var Button = function(_config){
		var _this = this;
		var config = {
			label : "按钮",
			type : "button",
			container : "body",
			onclick : null
		};
		$.extend(config,_config);	//合并用户的配置参数
		this.jQuery = null;
		var jq = {
			obj : null,
			left : null,
			label:null,
			right:null
		};
		var htmlBase = _.Html.button;	//html原型
		
		/**
		 * 显示出来
		 */
		this.display = function(){
			initjQuery();
			initEffect();
			_this.container(config.container);
			_this.label(config.label);
			_this.type(config.type);
			jq.obj.data("jDreamerObject",_this);
			return this;
		};
		
		/**
		 * 销毁当前对象
		 */
		this.destroy = function(){
			if(jq.obj){
				jq.obj.fadeOut(function(){
					jq.obj.remove();
					clearjQuery();
					_this = undefined;
					delete _this;
				});
			}
		};
		
		/**
		 * 按钮的类型
		 */
		this.type = function(_type){
			if(_type == undefined){
				return config.type;
			}else{
				config.type = _type;
				return this;
			}
		};
		
		/**
		 * 设置按钮上显示的文字
		 */
		this.label = function(_label){
			if(_label == undefined){
				return config.label;
			}else{
				config.label = _label;
				if(jq.obj){
					jq.label.html(config.label);
					_this.fitWidth();
				}
				return this;
			}
		};
		
		/**
		 * 自动调整大小
		 */
		this.fitWidth = function(){
			if(jq.obj){
				jq.label.width(1).css("overflow","auto");
				var width = jq.label.get(0).scrollWidth;
				jq.label.width(width).css("overflow","hidden");
				jq.obj.width(width + jq.left.width() + jq.right.width());
			}
		};
		
		/**
		 * 设置容器
		 * @param _container	容器
		 */
		this.container = function(_container){
			if(_container == undefined){
				return $(config.container);
			}else{
				config.container = _container;
				if(jq.obj){
					jq.obj.appendTo($(config.container));
				}
				return this;
			}
		};
		
		/**
		 * 初始化效果
		 */
		var initEffect = function(){
			if(jq.obj){
				jq.obj.mouseover(function(){
					jq.obj.addClass("jdmer_button_hover");
				}).mouseout(function(){
					jq.obj.removeClass("jdmer_button_hover");
					jq.obj.removeClass("jdmer_button_pressed");
				}).mousedown(function(){
					jq.obj.addClass("jdmer_button_pressed");
				}).mouseup(function(){
					jq.obj.removeClass("jdmer_button_pressed");
					if(config.type == "submit"){
						var form = jq.obj.parentFirstFind("form");
						form.submit();
					}else if(config.type == "reset"){
						var form = jq.obj.parentFirstFind("form");
						form.get(0).reset();
					}
					if(config.onclick){
						config.onclick();
					}
				});
			}
		};
		
		var clearjQuery = function(){
			jq.obj = null;
			_this.jQuery  = null;
			jq.left = null;
			jq.label = null;
			jq.right = null;
		};
		
		/**
		 * 初始化jQuery对象
		 */
		var initjQuery = function(){
			if(jq.obj == null){
				jq.obj = $(htmlBase);
				_this.jQuery = jq.obj;
				jq.left = jq.obj.children(".btn_left");
				jq.label = jq.obj.children(".btn_label");
				jq.right = jq.obj.children(".btn_right");
			}
		};
	};
	_.Button = Button;
	_.button = function(_config){
		return new _.Button(_config);
	};
	
	if(!_.Changer){
		return;
	}
	_.Changer.addHandler({
		name : "button",
		selector : "div[changer='button']",
		handler : function(objs , region){
			objs.each(function(i){
				var current = objs.eq(i);
				var container = current.parent();
				var config = current.attr("config");
				if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
				var label = current.html();
				if(label){config.label = label;}
				if(!config.container){
					config.container = container;
				}
				var button = _.button(config);
				button.display();
				button.jQuery.insertAfter(current);
				current.remove();
			});
		}
	});
})(jDreamer,jQuery);